import { NotFoundItem, Success } from "../../code/http-exception"
import auth from "../../middlewares/auth"
import { sequelize } from "../../sql/db";
import AfterSale from "../../sql/module/aftersale"
import ImgAfterSale from "../../sql/module/aftersale/img_after_sale";
import Customize from "../../sql/module/customize/index";
import StoreItem from "../../sql/module/factory/storeitem";
import User from "../../sql/module/user/user";
import { AddAfterSaleValidator } from "../../validators/afterSale"
import { InfoValidator } from "../../validators/public";
User.hasMany(AfterSale);
AfterSale.belongsTo(User);
User.hasMany(AfterSale);
AfterSale.belongsTo(User, { foreignKey: 'toUserId', as: 'toUser' });
StoreItem.hasMany(AfterSale);
AfterSale.belongsTo(StoreItem);
Customize.hasMany(AfterSale);
AfterSale.belongsTo(Customize);
const router: Api = new Map()

/**
 * 提交售后订单
 * @param StoreItemId number
 * @param CustomizeId number
 * @param problem string
 * @param preTime date
 * @param toUserId number
 * @returns 'ok'
 */
router.set('/addAfterSale', [auth('afterSale'), async (ctx) => {
    const v = await new AddAfterSaleValidator().validate(ctx)
    await sequelize.sync()
    const UserId = ctx.auth.id
    const StoreItemId = v.get('body.StoreItemId')
    const CustomizeId = v.get('body.CustomizeId')
    const problem = v.get('body.problem')
    const preTime = v.get('body.preTime')
    const toUserId = v.get('body.toUserId')
    await AfterSale.create({
        StoreItemId, CustomizeId, problem, preTime, UserId, toUserId
    })
    throw new Success()
}])

/**
 * 
 */
router.set('/afterSaleList', [auth('')])
/**
 * 
 */
router.set('/getAfterSaleInfoByStore', [auth('public'), async ctx => {
    const v = await new InfoValidator().validate(ctx)
    const UserId = ctx.auth.id
    const id = v.get('query.id')
    const afterSale = await AfterSale.findOne({
        attributes: {
            include: [[sequelize.literal('toUserId=' + UserId), 'isSelf']],
        },
        where: {
            StoreItemId: id,
            finish: false
        },
        include: {
            model: User, attributes: ['nickname'], as: 'toUser'
        }
    })
    if (!afterSale) { throw new NotFoundItem() }
    throw new Success(afterSale)
}])
router.set('/finish', [auth('public'), async (ctx) => {
    const v = await new InfoValidator().validate(ctx)
    const UserId = ctx.auth.id
    const id = v.get('query.id')
    const imgs = v.get('body.imgs')
    const resolve = v.get('body.resolve')
    const update = await AfterSale.update({
        finish: true,
        resolve
    }, {
        where: {
            id, toUserId: UserId
        }
    })
    if (update[0]) {
        if (imgs) {
            const count = await ImgAfterSale.count({ where: { AfterSaleId: id } })
            const new_imgs = imgs.slice(count, imgs.length)
            await ImgAfterSale.bulkCreate(new_imgs.map((item: any) => ({ AfterSaleId: id, url: item })))
        }
        throw new Success()
    } else {
        throw new NotFoundItem('权限不足')
    }
}])
export { router }